class AnswersController < ApplicationController

	before_filter :login_required, :only => [ :create, :update, :destroy ]

	def create
		@prayer = Prayer.find(params[:prayer_id])
		@answer = @prayer.answers.new(params[:answer])

		if @answer.save
			render :update do |page|
				page << "resetForm()"
				page.insert_html :top, 'answers', :partial => 'answers/answer', :locals => { :answer => @answer }
				page.replace dom_id(@prayer), :partial => 'prayers/prayer', :locals => { :prayer => @prayer }
			end
		else
			render :nothing => true
		end
	end

	def update
		@answer = Answer.find(params[:id])
		if @answer.update_attributes(params[:answer])
			render :nothing => true
		else
			render :nothing => true
		end
	end

	def destroy
		@prayer = Prayer.find(params[:prayer_id])
		@answer = Answer.find(params[:id])

		if @answer.owned_by?(@signed_user)
			@answer.destroy

			render :update do |page|
				page.remove dom_id(@answer)
				page.replace dom_id(@prayer), :partial => 'prayers/prayer', :locals => { :prayer => @prayer }
				page.replace "my_social_#{dom_id(@prayer)}", :partial => 'prayers/my_social_prayer', :locals => { :my_social_prayer => @prayer }
			end
		end
	end

end
